#include "../../include/DirScanner.h"
#include "../../include/Logger.h"
#include <iostream>
#include <sys/types.h>
#include <dirent.h>

using std::cout;
using std::endl;

DirScanner::DirScanner()
{

}

DirScanner::~DirScanner()
{

}

//重构函数调用运算符，调用traverse函数
void DirScanner::operator()(const string & dirname)
{
    traverse(dirname);
}

//返回_vecFilesfiles的引用
vector<string> &DirScanner::files()
{
    return _files;
}

//获取某一目录下的所有文件
void DirScanner::traverse(const string &dirname)
{
    DIR *pDir = opendir(dirname.c_str());
    if(!pDir){
        LogError("error: %s", dirname.c_str());
        return;
    }
    struct dirent *pdirent;
    while((pdirent = readdir(pDir)) != 0){
        string name = pdirent->d_name;//目录项名字
        if(name == "." || name == ".."){
            continue;
        }
        string subdir = dirname + "/" + name;//子目录路径
        if(pdirent->d_type == DT_DIR){
            //是目录
            traverse(subdir);//递归
        }
        else{
            //是文件
            _files.push_back(subdir);
        }
    }
    closedir(pDir);
}
